{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "from tqdm import tqdm\n",
    "import csv"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_data_path = '../data/testData0626.csv'\n",
    "port_path = '../data/port.csv'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def format_data_type(data):\n",
    "    data['onboardDate'] = pd.to_datetime(data['onboardDate'], infer_datetime_format=True).apply(lambda x: x.replace(tzinfo=None))\n",
    "    data['loadingOrder'] = data['loadingOrder'].astype(str)\n",
    "    data['timestamp'] = pd.to_datetime(data['timestamp'], infer_datetime_format=True).apply(lambda x: x.replace(tzinfo=None))\n",
    "    data['longitude'] = data['longitude'].astype(float)\n",
    "    data['latitude'] = data['latitude'].astype(float)\n",
    "    data['speed'] = data['speed'].astype(float)\n",
    "    data['TRANSPORT_TRACE'] = data['TRANSPORT_TRACE'].astype(str)\n",
    "    data['temp_timestamp'] = data['timestamp']\n",
    "    return data\n",
    "\n",
    "def get_test_data_info(path):\n",
    "    data = pd.read_csv(path) \n",
    "    test_trace_set = data['TRANSPORT_TRACE'].unique()\n",
    "    test_order_belong_to_trace = {}\n",
    "    for item in test_trace_set:\n",
    "        orders = data[data['TRANSPORT_TRACE'] == item]['loadingOrder'].unique()\n",
    "        test_order_belong_to_trace[item] = orders\n",
    "    \n",
    "    return format_data_type(data), test_trace_set, test_order_belong_to_trace\n",
    "\n",
    "test_data_origin, test_trace_set, test_order_belong_to_trace = get_test_data_info(test_data_path)\n",
    "\n",
    "def get_port_info():\n",
    "    port_data = {}\n",
    "    test_port_set = set()\n",
    "    for route in test_trace_set:\n",
    "        ports = route.split('-')\n",
    "        test_port_set = set.union(test_port_set, set(ports))\n",
    "    port_data_origin = pd.read_csv(port_path)\n",
    "    for item in port_data_origin.itertuples():\n",
    "        if getattr(item, 'TRANS_NODE_NAME') in test_port_set:\n",
    "            port_data[getattr(item, 'TRANS_NODE_NAME')] = {'LONGITUDE': getattr(item, 'LONGITUDE'),'LATITUDE': getattr(item, 'LATITUDE') }\n",
    "    return port_data\n",
    "port_data = get_port_info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "cnt1 = 0\n",
    "cnt2 = 0\n",
    "for route in test_trace_set:\n",
    "    start_port = route.split('-')[0]\n",
    "    start_lon = port_data[start_port]['LONGITUDE']\n",
    "    start_lat = port_data[start_port]['LATITUDE']\n",
    "    for order in test_order_belong_to_trace[route]:\n",
    "        order_info_set = test_data_origin[test_data_origin['loadingOrder'] == order].sort_values(by='timestamp')\n",
    "        onboardDate = test_data_origin[test_data_origin['loadingOrder'] == order]['onboardDate'].unique()[0]\n",
    "        start_time = test_data_origin[test_data_origin['loadingOrder'] == order]['timestamp'].unique()[0]\n",
    "        for row in order_info_set.itertuples():\n",
    "            if abs(row.longitude-start_lon) < 0.3 and abs(row.latitude-start_lat) < 0.3 and row.speed > 0:\n",
    "                start_time = row.timestamp\n",
    "                break  \n",
    "        row = order_info_set.head(1)\n",
    "\n",
    "        if (abs((row.longitude-start_lon).values[0]) > 0.3 or abs((row.latitude-start_lat).values[0]) > 0.3):\n",
    "            print('warning! positon error!  ', row.longitude.values[0], start_lon, row.latitude.values[0], start_lat, start_time, onboardDate)\n",
    "            cnt1 = cnt1 + 1\n",
    "        if start_time != onboardDate and int(pd.Timedelta(start_time-onboardDate).total_seconds()/3600) > 4:\n",
    "            mat = \"warning! time error!  {}\\t{}\\t{}\"\n",
    "            print(mat.format(start_time, onboardDate, pd.Timedelta(start_time-onboardDate).total_seconds()/3600))\n",
    "            cnt2 = cnt2 + 1\n",
    "        print(order)\n",
    "        print('----------------------------------------------------------------------------------------')\n",
    "        \n",
    "print(cnt1, cnt2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.7.6 64-bit ('AI': conda)",
   "language": "python",
   "name": "python37664bitaiconda6859e03b37c34f0182c9bde8073269f7"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}